import Taro from '@tarojs/taro'

const normalizeKey = (key: string, namespace = '') => {
  return namespace ? `${namespace}:${key}` : key
}

export const createStorage = (namespace?: string) => {
  return {
    getItem: (key: string) => {
      const val = Taro.getStorageSync(normalizeKey(key, namespace))

      console.log('[storage get]', key, val)

      return val
    },
    setItem: (key: string, value: any) => {
      console.log('[storage set]', key, value)

      Taro.setStorageSync(normalizeKey(key, namespace), value)
    },
    removeItem: (key: string) => {
      console.log('[storage remove]', key)
      Taro.removeStorageSync(normalizeKey(key, namespace))
    }
  }
}

export const storage = createStorage()
